package com.example.friend.aspect;



import com.example.core.constants.Constants;
import com.example.friend.domain.User;
import com.example.friend.manger.UserCacheManager;
import com.example.scurity.exception.ServiceException;
import com.example.scurity.utils.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 *
 * 用户状态检查
 * 拉黑状态禁止某些操作
 *
 */
@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    //配置织入点
    @Pointcut("@annotation(com.example.friend.annotation.CheckUserStatus)")
    public void aroundCut() {

    }

    @Before(value = "aroundCut()")
    public void before(JoinPoint point){
        User user = userCacheManager.getUserById(SecurityUtils.getUserId());
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        if (Objects.equals(user.getStatus(), Constants.FALSE)) {
            throw new ServiceException("账号异常请联系管理员申诉");
        }
    }

}
